﻿
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using Microsoft.AspNetCore.Builder;
using System.Net;
using RazorEngine.Compilation.ImpromptuInterface.Optimization;
using System.Threading;
using Admin.NET.Core.Util;
using Admin.NET.Application.Entity;
using Admin.NET.Application;
using Admin.NET.Core;

namespace Admin.NET.Web.Core;
public class SocketMiddleWare
{
    private readonly RequestDelegate _next;
    private static readonly int Port = 7777;
    private static UdpClient server;


    public SocketMiddleWare(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {

        
     
        //var udpFeature = new UdpConnectionFeature();
        //    features.Set<IUdpFeature>(udpFeature);

        //    // 处理UDP请求
        //    var reader = udpFeature.Connection.Transport.Input;
        //    var writer = udpFeature.Connection.Transport.Output;

        //    // 示例：简单的回复
        //    while (reader.TryRead(out var result))
        //    {
        //        await writer.WriteAsync(result.Buffer);
        //        reader.AdvanceTo(result.Buffer.End);
        //    }

        //    // 处理HTTP请求
        await _next(context);
    }
    public static void runThread()
    {

        Thread t = new Thread(udpThread);
        t.Start();
        //Thread Test =new Thread(testTread);
    }
    public static void udpThread()
    {
        try
        {
            server = new UdpClient(Port);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, Port);

            while (true)
            {
                //数据格式 前4位功能代码  2位,设备编号 (长度由设备编号长度决定) 1位 设备状态 2位 路由编号长度 路由编号(由路由编号决定)
                byte[] data = server.Receive(ref endPoint);
                string reply = "接收成功!!!";
                try
                {

                    SqlSugarRepository<ReceiveData> rep = new SqlSugarRepository<ReceiveData>();
                    SqlSugarRepository<BoardBase> rep1 = new SqlSugarRepository<BoardBase>();
                    BoardBaseService _boardBaseService = new BoardBaseService(rep1);
                    SqlSugarRepository<BoardRouteInfo> _rep2 = new SqlSugarRepository<BoardRouteInfo>();
                    BoardRouteInfoService _boardRouteInfoService = new BoardRouteInfoService(_rep2);
                    //rSvc = new ReceiveDataService(_boardBaseService, _boardRouteInfoService, rep);
                    ReceiveDataService rSvc = new ReceiveDataService(_boardBaseService, _boardRouteInfoService, rep);
                    rSvc.SaveData(data);
             
                    //ReceiveData receivedData = SocketUtil.formatData(data);
                    //string message = Encoding.UTF8.GetString(data);
                    //Console.WriteLine("接收到的数据:" + Util.Bytes2Hex(data));
                    //Console.WriteLine(receivedData.toStringData());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }


                data = Encoding.UTF8.GetBytes(reply);
                server.Send(data, data.Length, endPoint);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
    public static void testTread()
    {
        SqlSugarRepository<ReceiveData> rep = new SqlSugarRepository<ReceiveData>();
        SqlSugarRepository<BoardBase> rep1 = new SqlSugarRepository<BoardBase>();
        BoardBaseService _boardBaseService = new BoardBaseService(rep1);
        SqlSugarRepository<BoardRouteInfo> _rep2 = new SqlSugarRepository<BoardRouteInfo>();
        BoardRouteInfoService _boardRouteInfoService = new BoardRouteInfoService(_rep2);
        //rSvc = new ReceiveDataService(_boardBaseService, _boardRouteInfoService, rep);
        ReceiveDataService rSvc = new ReceiveDataService(_boardBaseService, _boardRouteInfoService, rep);
    }

}
public static class SocketMiddleWareExtensions
{
    public static IApplicationBuilder UseSocketMiddleWare(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<SocketMiddleWare>();
    }
}
